<?php 
require 'db.php';
//==================Hàm xóa==================================
if(isset($_POST['madoi']))$madoi=$_POST['madoi'];
if(isset($_POST['masv']))$masv=$_POST['masv'];
if(isset($_POST['del'])){
	$sql = "DELETE FROM dbo_doidangky WHERE MaSV=? and madoi=?";
	$stmt = $db->prepare ( $sql );
	$stmt->bindParam ( "1", $masv );
	$stmt->bindParam ( "2", $madoi );
	$stmt->execute ();
	$success=$stmt->rowCount();
	if(!$success){
		echo "Có lỗi xảy ra vui lòng thử lại sau nhá";
	}else{
		echo 1;
		exit;
	}
}

//====================================================
if(isset($_POST['AddMonTheThaoDoi'])){
		if (isset($_REQUEST["MADOI"])){
			$maDoi = $_REQUEST["MADOI"];
			$maSV = $_REQUEST["masv"];
			$MaMonTheThaoDoi = explode (",", $maDoi);
			foreach ( $MaMonTheThaoDoi as $mamttd ){
				if(!empty($mamttd)){
					$sql = "INSERT INTO dbo_doidangky(MaSV,MaDoi,ThanhTich,SDTDK) VALUES ('". $maSV. "','" . $mamttd . "','','')";
					$result = $db->exec( $sql );
				}
			}
 			echo "Đăng ký thành công";
			exit();
		}
	}
//====================================================
if(isset($_POST['manhom'])) $manhom=$_POST['manhom'];
	$noidung = array();
	$sqldk="SELECT  dbo_doidangky.MADOI,TENDOI,SLTOIDA,SLTOIDA - count(MASV) as SVDKT ";
	$sqldk.="from dbo_dongdoi,dbo_doidangky,dbo_noidung
where dbo_dongdoi.MANHOM=:manhom and dbo_dongdoi.MADOI=dbo_doidangky.MADOI and dbo_noidung.MANHOM=dbo_dongdoi.MANHOM ";
	$sqldk.=" group by MADOI";
	$sqldk.=" union (";
	$sqldk.=" SELECT  MADOI,TENDOI,SLTOIDA,SLTOIDA as SVDKT ";
	$sqldk.=" from dbo_dongdoi,dbo_noidung 
where dbo_dongdoi.MANHOM=:manhom and dbo_noidung.MANHOM=dbo_dongdoi.MANHOM  and  MADOI not in (  ";
	$sqldk.=" SELECT  dbo_doidangky.MADOI ";
	$sqldk.=" from dbo_dongdoi,dbo_doidangky,dbo_noidung 
where dbo_dongdoi.MANHOM=:manhom and dbo_dongdoi.MADOI=dbo_doidangky.MADOI and dbo_noidung.MANHOM=dbo_dongdoi.MANHOM )) ";
	$stmt = $db->prepare($sqldk);
	$stmt->bindParam("manhom",$manhom,PDO::PARAM_STR);
	$stmt->execute();
	$stmt->execute();
	if($stmt->execute()){
	while($r=$stmt->fetch()){
		$noidung=array_merge($noidung,array(
				array("MADOI"=>$r['MADOI'],"TENDOI"=>$r['TENDOI'],"SLTOIDA"=>$r['SLTOIDA'],"SVDKT"=>$r['SVDKT'])
		));
	}}

//=================Xay dung ham kiem tra ==================================
	$kiemtra = array();
	if(isset($_POST['masv']))$masv=$_POST['masv'];
	$sqlkt="select dbo_doidangky.MADOI,TENDOI,SLTOIDA,SLTOIDA - count(MASV) as SVDKT";
	$sqlkt.=" from dbo_dongdoi,dbo_doidangky,dbo_noidung";
	$sqlkt.=" where dbo_dongdoi.MANHOM=:manhom and dbo_dongdoi.MADOI=dbo_doidangky.MADOI and dbo_noidung.MANHOM=dbo_dongdoi.MANHOM  and dbo_doidangky.MASV=:masv";
	$sqlkt.=" group by MADOI";
	$stmtkt = $db->prepare($sqlkt);
	$stmtkt->bindParam('manhom',$manhom,PDO::PARAM_STR);
	$stmtkt->bindParam('masv',$masv,PDO::PARAM_STR);
	$stmtkt->execute();
	if($stmtkt->execute()){
		while($rkt=$stmtkt->fetch()){
			$kiemtra=array_merge($kiemtra,array(
					array("MADOI"=>$rkt['MADOI'])
			));
		}}

//==================================================== 
?>
<div id="khungmonthi">
  <table  class="table table-bordered table-hover" style="font-size: 13px;">
    <thead>

      <tr class="info">
	  	<th>STT</th>  
		<th>Mã Đội </th> 
        <th>Danh sách cái đội </th>
        <th>Số lượng thành viên đội</th>
		<th>Số lượng còn lại</th>
       	<th>Chọn đội</th>

      </tr>
    </thead>
    <tbody>
			<?php $i=1;
			foreach($noidung as $nd){?>
      	<tr>
	  	<td><?php echo $i++;?></td>
        <td><?php echo $nd['MADOI'];?></td>
        <td><?php echo $nd['TENDOI'];?></td>
		<td><?php echo $nd['SLTOIDA'];?></td>
		<td><?php echo $nd['SVDKT'];?></td>
        <td>
		<?php 	
			$dk=false;
				foreach($kiemtra as $kt){
					if($kt['MADOI']==$nd['MADOI']){$dk=true;}	
				}
			if($dk) echo"Đã đăng kí" ;
			else if($nd['SVDKT'] < 1) echo"Đã đủ số lượng";
					else echo "<input name='chkd[]'  value='" . $nd['MADOI'] . "' class='chkd' type='checkbox'/>";
		?>
		</td>
        <?php }?>

      </tr>
    </tbody>
    <tfoot>
      <tr>
      </tr>
    </tfoot>
  </table>
  <hr />
  <div class="form-group">
    <div class="col-sm-5"></div>
    <div class="col-sm-3">
      <button  class="form-control btn btn-primary btn-xs" id="AddMonTheThaoDoi" value="show"> Đăng ký </button>
    </div>
  </div>
</div>

